(function (window, $, undefined) {
  'use strict';

  var $timeRange = $('#timeRange');
  var $total = $('#total');

  var $tableList = $('#table-list');

  main(function () {

    laydate.render({
      range: '至',
      elem: '#timeRange'
    });

    $('#btn-search').click(function () {
      var val = $timeRange.val();
      $total.text('-');
      if (val) {
        var temp = val.split('至');
        var data = {
          beginTime: util.getTime2($.trim(temp[0])),
          endTime: util.getTime2($.trim(temp[1]), true)
        };

        util.get(ajaxURL.ORDER + '/pay/queryTotalAmount', data, function (res) {
          $total.html(((res.data || 0) / 100) + ' 元');
        });

        // util.get(ajaxURL.ORDER + '/order/getFinishOrderList', {
        //   orderStateIn: '10,11,12',
        //   createTimeBegin: data.beginTime,
        //   createTimeEnd: data.endTime,
        //   pageNum: 1,
        //   pageSize: 99999999
        // }, function (res) {
        //   var ret = 0;
        //   $.each(res.data, function (index, item) {
        //     ret += item.factFinalPrice;
        //   });
        //   $total.html((ret / 100) + ' 元');
        // });

        util.searchGrid($tableList, {
          orderStateIn: '10,11,12',
          createTimeBegin: data.beginTime,
          createTimeEnd: data.endTime
        });
      } else {
        util.tip.warn('请先选择一个时间范围！');
      }

    });

    $('#btn-export').click(function () {
      var val = $timeRange.val();
      if (val) {
        var temp = val.split('至');
        var data = {
          orderStateIn: '10,11,12',
          createTimeBegin: util.getTime2($.trim(temp[0])),
          createTimeEnd: util.getTime2($.trim(temp[1]), true)
        };

        layer.confirm('确定导出当前选择的时间范围的数据？', function () {
          layer.closeAll();
          $('#btn-search').click();
          util.get(ajaxURL.ORDER + '/order/exportFinishOrderList', data, function (res) {
            var fileUrl = 'http://file.hangkechina.com/' + res.data;
            location.href = fileUrl;
          });
        });
      } else {
        util.tip.warn('请先选择一个时间范围！');
      }
    });

    util.jqGrid($tableList, {
      url: ajaxURL.ORDER + '/order/getFinishOrderList',
      autowidth: true,
      height: $(window).height() - 400,
      shrinkToFit: false,
      colModel: [
        { label: '订单编号', key: true, name: 'orderNo', width: 150 },
        { label: '司机', name: 'driverName', width: 90 },
        { label: '车牌', name: 'autoNo', width: 80 },
        {
          label: '订单创建时间', name: 'createTime', width: 150, formatter: function (v) {
            return util.dateFormat(v);
          }
        },
        { label: '用时', name: 'driveTime', width: 100, formatter: function (v, o, r) {
          return Math.floor(v / 60) + ' 分钟'
        } },
        {
          label: '出发地', name: 'actualOrig', width: 200
        },
        {
          label: '目的地', name: 'actualDest', width: 200
        },
        {
          label: '里程', name: 'driveMile', width: 80, formatter: function (v) {
            return v / 1000 + ' 公里';
          }
        },
        { label: '支付价格', name: 'factFinalPrice', width: 100, formatter: function (v) {
          return (v / 100) + ' 元'
        } },
        { label: '乘客电话', name: 'passengerPhone', width: 100 },
        { label: '支付方', name: 'payType', width: 80, formatter: function (v) {
          return +v === 1 ? '乘客支付' : '司机代付'
        } },
        { label: '支付方式', name: 'payChannel', width: 80, formatter: function (v, o, r) {
          // 存在司机代付的情况，取消修复设置
          // if (r.orderChannel === 1) { // 小程序支付方式修复
          //   v = 1;
          // }
          return {
              '1': '微信',
              '2': '支付宝',
              '3': '银联',
              '4': '账户扣款',
              '5': '企业代扣',
              '9': '其他'
          }[v] || '';
        }}
      ],
      pager: '#pager-list'
    });

  });


})(window, jQuery);
